#include <work_queue.hpp>
#include <work_queue_run.hpp>

WorkQueueRun::WorkQueueRun(int priority, int stackSize, int rate, const char* taskName, int cpuId)
	: TaskRun(priority, stackSize, taskName, cpuId)
	, m_SemRw(1)
	, m_SleepMS(1000 / rate)
{
	if (m_SleepMS <= 0)
	{
		m_SleepMS = 10;
	}
}

WorkQueueRun::~WorkQueueRun()
{
}

void WorkQueueRun::Append(WorkQueue* workQueue)
{
	if (workQueue != nullptr)
	{
		m_SemRw.Wait();
		m_WQList.push_back(workQueue);
		m_SemRw.Post();
	}
}

void WorkQueueRun::Run(void* arg)
{
	while (1)
	{
		m_SemRw.Wait();
		for (WorkQueue* wq : m_WQList)
		{
			if (wq != nullptr)
			{
				wq->RunImpl(wq->m_Arg);
				wq->m_Tk++;
			}
		}
		m_SemRw.Post();
		TaskRun::mSleep(m_SleepMS);
	}
}